import axios from 'axios';
import { Modal, message } from 'antd';

let baseApi = ''
if(process.env.NODE_ENV==='production'){
    baseApi=process.env.REACT_APP_PRODUCTION_BASE_URL
}else if(process.env.NODE_ENV==='development'){
    baseApi=process.env.REACT_APP_DEVELOPMENT_BASE_URL
}

export default{
    ajax: function(options){
        let headers = {
            "Content-Type": "application/json;charset=UTF-8"
        }
        if(localStorage.getItem('token')){
            headers.Authorization = localStorage.getItem('token')
        }
        return new Promise((resolve, reject)=>{
             axios({
                 url: options.url,
                 method: options.method || 'get',
                 baseURL: baseApi,
                 timeout: 5000,
                 data: options.data || '',
                 headers
             }).then((response)=>{
                 if(response.data.code == 1){
                    resolve(response.data)
                 }else{
                    message.info(response.data.msg);
                    resolve(response.data)
                 }

                 if(response.data.code == -2){
                    message.info('请重新登录');
                    window.location.hash = '#/login'
                 }
             })
        })
    },

    get: (options) => {
        let headers = {
            "Content-Type": "application/json;charset=UTF-8"
        }
        if(localStorage.getItem('token')){
            headers.Authorization = localStorage.getItem('token')
        }
        return new Promise((resolve)=>{
             axios({
                 url: options.url,
                 method: options.method || 'get',
                 baseURL: baseApi,
                 timeout: 5000,
                 headers,
                 params: options.params
             }).then((response)=>{
                 if(response.data.code == 1){
                    resolve(response.data)
                 }else{
                    message.info(response.data.msg);
                    resolve(response.data)
                 }

                 if(response.data.code == -2){
                    message.info('请重新登录');
                    window.location.hash = '#/login'
                 }
             })
        })
    },

    upData: (options) => {
        let headers = {
            "Content-Type": "application/json;charset=UTF-8"
        }
        console.log(localStorage.getItem('token'));
        if(localStorage.getItem('token')){
            headers.Authorization = localStorage.getItem('token')
        }
        console.log(headers);
        return new Promise((resolve, reject)=>{
            axios({
                url: '/Upload/image',
                method: 'post',
                baseURL: '/common',
                timeout: 5000,
                data: options.data,
                headers
            }).then((response)=>{
                if(response.data.code == 1){
                    resolve(response.data)
                }else{
                    message.info(response.data.msg);
                    resolve(response.data)
                }

                if(response.data.code == -2){
                    message.info('请重新登录');
                    window.location.hash = '#/login'
                }
            })
        })
    }
}